﻿using System;
using System.IO;
using System.Windows.Forms;

namespace SubtitleAdjuster
{
    public partial class Form1 : Form
    {
        private int offset = 0;
        private string filter = "Supported Subtitle Files|*.srt|All Files|*.*";

        public Form1()
        {
            InitializeComponent();
            txtOffset.Text = offset.ToString();
            openFileDialog1.Filter = filter;
            saveFileDialog1.Filter = filter;
            openFileDialog1.FileName = String.Empty;
            saveFileDialog1.FileName = "new_subtitle.srt";
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            txtInputFile.Text = openFileDialog1.FileName;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            txtOutputFile.Text = saveFileDialog1.FileName;
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtInputFile.Text) &&
                !String.IsNullOrEmpty(txtOutputFile.Text) &&
                !String.IsNullOrEmpty(txtOffset.Text))
            {
                if (File.Exists(txtInputFile.Text))
                {
                    ConvertTimings();
                }
                else
                {
                    MessageBox.Show(txtInputFile.Text + ": File does not exist.");
                    return;
                }
            }
            else
            {
                MessageBox.Show("All fields must be filled in to convert timings.");
                return;
            }
        }

        private void ConvertTimings()
        {
            string[] tempStr;
            String line;
            FileStream fileInput = File.OpenRead(txtInputFile.Text);
            FileStream fileOutput = new FileStream(txtOutputFile.Text, FileMode.Create);

            try
            {
                offset = Convert.ToInt32(txtOffset.Text);
            }
            catch
            {
                MessageBox.Show("You must enter a valid integer into the Offset box.");
                return;
            }
            using (StreamReader sr = new StreamReader(fileInput))
            {
                using (StreamWriter sw = new StreamWriter(fileOutput))
                {
                    while (!sr.EndOfStream)
                    {
                        line = sr.ReadLine();//.Split(" --> ");
                        if (line.Contains(" --> "))
                        {
                            tempStr = new string[2];
                            tempStr[0] = line.Substring(0, line.IndexOf(" --> "));
                            tempStr[1] = line.Substring(line.IndexOf(" --> ") + 5);
                        }
                        else
                        {
                            tempStr = new string[1];
                            tempStr[0] = line;
                        }
                        if (tempStr.Length == 1) sw.WriteLine(tempStr[0]);
                        else
                        {
                            for (int i = 0; i < tempStr.Length; i++)
                            {
                                string[] times = tempStr[i].Split(',');
                                string[] time = times[0].Split(':');
                                int hour = Convert.ToInt16(time[0]);
                                int minute = Convert.ToInt16(time[1]);
                                int second = Convert.ToInt16(time[2]);
                                int ms = Convert.ToInt16(times[1]);
                                DateTime newDt = new DateTime(1, 1, 1, hour, minute, second, ms);
                                newDt = newDt.AddMilliseconds(Convert.ToDouble(offset));
                                if (i == 0) sw.Write("{0:00}:{1:00}:{2:00},{3:000} --> ",
                                    newDt.Hour, newDt.Minute, newDt.Second, newDt.Millisecond);
                                else
                                    sw.WriteLine("{0:00}:{1:00}:{2:00},{3:000}",
                                        newDt.Hour, newDt.Minute, newDt.Second, newDt.Millisecond);
                            }
                        }
                    }
                    fileOutput.Flush();
                    MessageBox.Show("Finished writing new .SRT file: " + txtOutputFile.Text);
                }
            }
            txtInputFile.Text = String.Empty;
            txtOutputFile.Text = String.Empty;
            offset = 0;
            txtOffset.Text = offset.ToString();
        }
    }
}
